
package com.czie.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.videogo.errorlayer.ErrorInfo;
import com.videogo.exception.BaseException;
import com.videogo.openapi.EZConstants;
import com.videogo.openapi.EZOpenSDK;
import com.videogo.openapi.EZPlayer;
import com.videogo.openapi.bean.EZCameraInfo;
import com.videogo.openapi.bean.EZDeviceInfo;

import java.util.List;

public class CameraVideoActivity extends AppCompatActivity implements View.OnClickListener {

    private SurfaceView camera;
    private TextView cameraTitleTextView;
    // 方向键
    //  private DLRoundMenuView dlRoundMenuView;
    private ImageView uparrow, leftarrow, rightarrow, downarrow;
    // 拉近拉远按钮
    private Button buttonIn, buttonOut;

    // 萤石摄像头
    String mAccessToken = "at.71d9vf9r05625e1z2fur3tmub9fy3ahi-6bfqpp06eo-1ur30xp-q0noip7gt";
    String uuid = "113694134";
    String name = "8号实验柜";
    String cameraAppKey = "a550895fa82d400aaf75b3d3e3f814a7";
    private static final int MSG_ON_DEVICE_RESPONSE = 1;
    private List<EZDeviceInfo> mDeviceInfoList;
    private EZPlayer mEZPlayer = null;
    private SurfaceHolder mRealPlaySh;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera_video);
        Log.e("配置", "mAccessToken = " + mAccessToken);
        Log.e("配置", "uuid = " + uuid);
        Log.e("配置", "name = " + name);
        uparrow = findViewById(R.id.uparrow);
        leftarrow = findViewById(R.id.leftarrow);
        rightarrow = findViewById(R.id.rightarrow);
        downarrow = findViewById(R.id.downarrow);
        buttonIn = findViewById(R.id.buttonIn);
        buttonOut = findViewById(R.id.buttonOut);
        uparrow.setOnClickListener(this);
        leftarrow.setOnClickListener(this);
        rightarrow.setOnClickListener(this);
        downarrow.setOnClickListener(this);
        buttonIn.setOnClickListener(this);
        buttonOut.setOnClickListener(this);
        // 配置萤石云
        // 初始化萤石SDK
        EZOpenSDK.initLib(getApplication(), cameraAppKey);
        // 初始化萤石SDK
        EZOpenSDK.getInstance().setAccessToken("at.71d9vf9r05625e1z2fur3tmub9fy3ahi-6bfqpp06eo-1ur30xp-q0noip7gt");

        EZOpenSDK.getInstance().setAccessToken(mAccessToken);
        initView();
        mRealPlaySh = camera.getHolder();

        mRealPlaySh.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                if (mEZPlayer != null) {
                    mEZPlayer.setSurfaceHold(surfaceHolder);
                }
                mRealPlaySh = surfaceHolder;
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                Log.e("surfaceChanged", "摄像头进行转动");
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                if (mEZPlayer != null) {
                    mEZPlayer.setSurfaceHold(null);
                }
                mRealPlaySh = null;
            }
        });

        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    mDeviceInfoList = EZOpenSDK.getInstance().getDeviceList(0, 20);

                    mHandler_camera.sendEmptyMessage(MSG_ON_DEVICE_RESPONSE);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    // 控制摄像头
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            // TODO:添加摄像头的云台控制操作
            case R.id.uparrow://顶部
                upMove();
                Log.e("摄像头控制", "0号按钮：上");
                break;
            case R.id.rightarrow://右边
                rightMove();
                Log.e("摄像头控制", "1号按钮：右");
                break;
            case R.id.downarrow://下面
                downMove();
                Log.e("摄像头控制", "2号按钮：下");
                break;
            case R.id.leftarrow://左边
                leftMove();
                Log.e("摄像头控制", "3号按钮：左");
                break;
            case R.id.buttonIn://拉近
                ZoomIn();
                Log.e("摄像头控制", "摄像头拉近");
                break;
            case R.id.buttonOut://拉远
                ZoomOut();
                Log.e("摄像头控制", "摄像头拉远");
                break;
        }
    }

    // 初始化界面控件
    private void initView() {
        cameraTitleTextView = findViewById(R.id.cameraTitleTextView);
        cameraTitleTextView.setText(name);
        camera = findViewById(R.id.camera);
        //  dlRoundMenuView = findViewById(R.id.dlRoundMenuView);
        //   dlRoundMenuView.setOnMenuClickListener(new OnMenuClickListener() {
     /*       @Override
            public void OnMenuClick(int position) {
                // TODO:添加摄像头的云台控制操作
                switch (position) {
                    case 0://顶部
                        upMove();
                        Log.e("摄像头控制", "0号按钮：上");
                        break;
                    case 1://右边
                        rightMove();
                        Log.e("摄像头控制", "1号按钮：右");
                        break;
                    case 2://下面
                        downMove();
                        Log.e("摄像头控制", "2号按钮：下");
                        break;
                    case 3://左边
                        leftMove();
                        Log.e("摄像头控制", "3号按钮：左");
                        break;
                }

            }
        });
        */
    }

    // 播放实时视频
    private void startPlay() {
        EZDeviceInfo deviceInfo = null;
        for (EZDeviceInfo ezDeviceInfo : mDeviceInfoList) {
            Log.d("EZDeviceInfo", ezDeviceInfo.getDeviceName() + ";;SN=" + ezDeviceInfo.getDeviceSerial() + ";; type=" + ezDeviceInfo.getDeviceType());
            if (uuid.equals(ezDeviceInfo.getDeviceSerial())) {
                deviceInfo = ezDeviceInfo;
                break;
            }
        }
        if (deviceInfo == null) {
            return;
        }

        EZCameraInfo cameraInfo = getCameraInfoFromDevice(deviceInfo, 0);
        mEZPlayer = EZOpenSDK.getInstance().createPlayer(cameraInfo.getDeviceSerial(), cameraInfo.getCameraNo());

        if (mEZPlayer == null)
            return;
        if (cameraInfo == null) {
            return;
        }

        mEZPlayer.setHandler(mHandler_camera);
        mEZPlayer.setSurfaceHold(mRealPlaySh);
        mEZPlayer.startRealPlay();
    }

    /**
     * 通过ezdevice 得到其中通道信
     *
     * @param deviceInfo
     * @return
     */
    public static EZCameraInfo getCameraInfoFromDevice(EZDeviceInfo deviceInfo, int camera_index) {
        if (deviceInfo == null) {
            return null;
        }
        if (deviceInfo.getCameraNum() > 0 && deviceInfo.getCameraInfoList() != null && deviceInfo.getCameraInfoList().size() > camera_index) {
            return deviceInfo.getCameraInfoList().get(camera_index);
        }
        return null;
    }

    @SuppressLint("HandlerLeak")
    private Handler mHandler_camera = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case MSG_ON_DEVICE_RESPONSE:
                    startPlay();
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_SUCCESS:
//                    播放成功
//                    Bitmap bitmap = takePhoto();
//                    if (bitmap==null){
//                        Log.e("@@@@@@@@@@","nullllll");
//                    }
                    break;
                case EZConstants.EZRealPlayConstants.MSG_REALPLAY_PLAY_FAIL:
                    // 视频播放失误进行接收
//                    //播放失败,得到失败信息
                    ErrorInfo errorinfo = (ErrorInfo) msg.obj;
//                    //得到播放失败错误码
                    int code = errorinfo.errorCode;
//                    //得到播放失败模块错误码
                    String codeStr = errorinfo.moduleCode;
//                    //得到播放失败描述
                    String description = errorinfo.description;
//                    //得到播放失败解决方方案
                    String sulution = errorinfo.sulution;
                    Log.e("视频失误", "codeStr=" + codeStr + ",description=" + description + ",sulution=" + sulution);
                    break;
//                case 50:
//                    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1, list);
//                    faceData.setAdapter(adapter);
//                    break;
                case 51:

                    break;
                case 9:
//                    EZOpenSDK.getInstance().setAccessToken(mAccessToken);
                    break;
            }
        }
    };

    /**
     * 摄像头上移操作
     */
    private void upMove() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandUp, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 摄像头右移操作
     */
    private void rightMove() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandRight, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 摄像头下移操作
     */
    private void downMove() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandDown, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * 摄像头左移操作
     */
    private void leftMove() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandLeft, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * TODO：保留操作 镜头拉远
     */
    private void ZoomOut() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandZoomOut, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandZoomOut, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

    /**
     * TODO：保留操作 镜头拉近
     */
    private void ZoomIn() {
        new Thread() {
            @Override
            public void run() {
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandZoomIn, EZConstants.EZPTZAction.EZPTZActionSTART, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                try {
                    EZOpenSDK.getInstance().controlPTZ(uuid, 1, EZConstants.EZPTZCommand.EZPTZCommandZoomIn, EZConstants.EZPTZAction.EZPTZActionSTOP, 1);
                } catch (BaseException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }


}
